//
//  ViewController.swift
//  SimpleAnimations
//
//  Created by robin shie on 15/2/7.
//  Copyright (c) 2015年 shie robin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
//状态
    var hamurgerOpen = true
    var hamurgerButton : EkoButton?
    var top:UIView?
    var middle:UIView?
    var bottom:UIView?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.hamurgerButton = EkoButton()
        self.hamurgerButton?.backgroundColor = UIColor.blackColor()
        self.hamurgerButton?.frame = CGRectMake(100, 100, 150, 150)
        self.hamurgerButton?.layer.cornerRadius = 75
        self.view.addSubview(hamurgerButton!)
        self.hamurgerButton?.addTarget(self, action: "didTapHamburgerButton:", forControlEvents: UIControlEvents.TouchUpInside)
        //实例化三道杠
        let sectionWidth:CGFloat = 80.0
        let sectionHeight:CGFloat = 11.0
        top = UIView(frame: CGRectMake(self.hamurgerButton!.bounds.size.width/2 - sectionWidth/2, 40, sectionWidth, sectionHeight))
        top!.backgroundColor = UIColor.whiteColor()
        top!.userInteractionEnabled = false
        top!.layer.cornerRadius = sectionHeight/2
        self.hamurgerButton?.addSubview(top!)
        
        middle = UIView(frame: CGRectMake(self.hamurgerButton!.bounds.size.width/2 - sectionWidth/2, 70, sectionWidth, sectionHeight))
       middle!.backgroundColor = UIColor.whiteColor()
        middle!.userInteractionEnabled = false
        middle!.layer.cornerRadius = sectionHeight/2
        self.hamurgerButton?.addSubview(middle!)
        
        bottom = UIView(frame: CGRectMake(self.hamurgerButton!.bounds.size.width/2 - sectionWidth/2, 100, sectionWidth, sectionHeight))
        bottom!.backgroundColor = UIColor.whiteColor()
        bottom!.userInteractionEnabled = false
        bottom!.layer.cornerRadius = sectionHeight/2
        self.hamurgerButton?.addSubview(bottom!)
        
    }
    func didTapHamburgerButton(sender:UIButton){
        //pop对象复用
        //颜色
        var topColor = self.top?.pop_animationForKey("topColor") as POPSpringAnimation?
        var bottomColor = self.top?.pop_animationForKey("bottomColor") as POPSpringAnimation?
        //角度
        var topRotate = self.top?.layer.pop_animationForKey("topRotate") as POPSpringAnimation?
        var bottomRotate = self.top?.layer.pop_animationForKey("bottomRotate") as POPSpringAnimation?
        //位移
        var topPosition = self.top?.layer.pop_animationForKey("topPosition") as POPSpringAnimation?
        var bottomPosition = self.top?.layer.pop_animationForKey("bottomPosition") as POPSpringAnimation?
        if hamurgerOpen {
            //bianX
            hamurgerOpen = false
            UIView.animateWithDuration(0.2, animations: { () -> Void in
              self.middle!.alpha = 0
            })
        //边上下颜色
            if topColor != nil {
              topColor?.toValue = UIColor.redColor()
            }else{
               topColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
                topColor?.toValue = UIColor.redColor()
                topColor?.springBounciness = 0
                topColor?.springSpeed = 18
                top?.pop_addAnimation(topColor, forKey: "topColor")
            }
            if bottomColor != nil {
                bottomColor?.toValue = UIColor.redColor()
            }else{
                bottomColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
                bottomColor?.toValue = UIColor.redColor()
                bottomColor?.springBounciness = 0
                topColor?.springSpeed = 18
                bottom?.pop_addAnimation(topColor, forKey: "bottomColor")
            }
            
            //旋转上下两脚角度
            if topRotate != nil{
              topRotate?.toValue = -M_PI/4
            }else{
             topRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)
             topRotate?.toValue = -M_PI/4
             topRotate?.springBounciness = 11
             topRotate?.springSpeed = 18
             top?.layer.pop_addAnimation(topRotate, forKey: "topRotate")
            }
            
            if bottomRotate != nil{
                bottomRotate?.toValue = M_PI/4
            }else{
                bottomRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)
                bottomRotate?.toValue = M_PI/4
                bottomRotate?.springBounciness = 11
                bottomRotate?.springSpeed = 18
                bottom?.layer.pop_addAnimation(bottomRotate, forKey: "bottomRotate")
            }
            //改变上下红x的y坐标
            if topPosition != nil {
              topPosition?.toValue = 29
            }else{
                topPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)
                topPosition?.toValue = 29
                topPosition?.springBounciness = 11
                topPosition?.springSpeed = 18
                top?.layer.pop_addAnimation(topPosition, forKey: "topPosition")
            }
            
            if bottomPosition != nil {
                bottomPosition?.toValue = -29
            }else{
                bottomPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)
                bottomPosition?.toValue = -29
                bottomPosition?.springBounciness = 11
                bottomPosition?.springSpeed = 18
                bottom?.layer.pop_addAnimation(bottomPosition, forKey: "bottomPosition")
            }
            
        }else{
               //边三角角度
            hamurgerOpen = true
            
            UIView.animateWithDuration(0.2, animations: { () -> Void in
                self.middle!.alpha = 1
            })
            //边上下颜色
            if topColor != nil {
                topColor?.toValue = UIColor.purpleColor()
            }else{
                topColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
                topColor?.toValue = UIColor.purpleColor()
                topColor?.springBounciness = 0
                topColor?.springSpeed = 18
                top?.pop_addAnimation(topColor, forKey: "topColor")
            }
            if bottomColor != nil {
                bottomColor?.toValue = UIColor.purpleColor()
            }else{
                bottomColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
                bottomColor?.toValue = UIColor.purpleColor()
                bottomColor?.springBounciness = 0
                topColor?.springSpeed = 18
                bottom?.pop_addAnimation(topColor, forKey: "bottomColor")
            }
            
            //旋转上下两脚角度
            if topRotate != nil{
                topRotate?.toValue = 0
            }else{
                topRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)
                topRotate?.toValue = 0
                topRotate?.springBounciness = 11
                topRotate?.springSpeed = 18
                top?.layer.pop_addAnimation(topRotate, forKey: "topRotate")
            }
            
            if bottomRotate != nil{
                bottomRotate?.toValue = 0
            }else{
                bottomRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)
                bottomRotate?.toValue = 0
                bottomRotate?.springBounciness = 11
                bottomRotate?.springSpeed = 18
                bottom?.layer.pop_addAnimation(bottomRotate, forKey: "bottomRotate")
            }
            //改变上下红x的y坐标
            if topPosition != nil {
                topPosition?.toValue = 0
            }else{
                topPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)
                topPosition?.toValue = 0
                topPosition?.springBounciness = 11
                topPosition?.springSpeed = 18
                top?.layer.pop_addAnimation(topPosition, forKey: "topPosition")
            }
            
            if bottomPosition != nil {
                bottomPosition?.toValue = 0
            }else{
                bottomPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)
                bottomPosition?.toValue = 0
                bottomPosition?.springBounciness = 11
                bottomPosition?.springSpeed = 18
                bottom?.layer.pop_addAnimation(bottomPosition, forKey: "bottomPosition")
            }

        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

